package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.ShoppingCar;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.query.StrategyCommentQueryObject;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.query.UserQueryObject;
import cn.wolfcode.trip.base.service.*;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import cn.wolfcode.trip.base.query.*;
import cn.wolfcode.trip.base.service.*;

import java.util.List;

import java.util.Map;


/**
 * 用户注册资源接口
 */
@RestController
@RequestMapping("users")
public class UserController {
    @Autowired
    private IUserService userService;
    @Autowired
    private ITravelService travelService;
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IStrategyCommentService strategyCommentService;
    @Autowired
    private ITravelReviewService travelReviewService;
    @Autowired
    private IAttentionService attentionService;

    @Autowired
    private IShoppingCarService shoppingCarService; //购物车service
    /**
     * 注册
     * @param user
     * @return
     */
    @PostMapping
    public JSONResult regist(User user){
        JSONResult jsonResult = new JSONResult();
        try {
            //注册用户(新增)
            userService.saveOrUpdate(user);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.mark(e.getMessage());
        }
        return jsonResult;
    }

    /**
     * 修改某个用户信息
     * @param user
     * @return
     */
    @PutMapping("{id}")
    public JSONResult update (User user){

        JSONResult jsonResult = new JSONResult();
        userService.saveOrUpdate(user);
        //返回给页面 用来回显
        jsonResult.setResult(user);
        return jsonResult;
    }


    /**
     * 主页 查询
     * @param qo
     * @return
     */
    @GetMapping("{authorId}/travels")
    public PageInfo queryTravel(TravelQueryObject qo){
        qo.setOrderBy("t.lastUpdateTime desc");
        return travelService.queryTravel(qo);
    }

    /**
     * 查询用户信息
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public User selectUserById(@PathVariable("id") Long id){
        return userService.getUser(id);
    }

    /**
     * 返回单个用户所有的攻略一级评论，需要分页高查
     * @return
     */
    @GetMapping("/{userId}/comments")
    public PageInfo queryComments(StrategyCommentQueryObject qo){
        qo.setOrderBy("sc.createTime desc");
        return strategyCommentService.queryCommentsByUserId(qo);
    }

    /**
     * 返回单个用户所有的游记评论，会分页高查
     * @return
     */
    @GetMapping("/{userId}/travelComments")
    public PageInfo queryTravelComments(TravelReviewQueryObject qo){
        qo.setOrderBy("travelreview.createTime");
        return travelReviewService.getCommentById(qo);
    }

    /**
     * 查询用户的id查询收藏的攻略
     * @param qo
     * @return
     */

    @GetMapping("{id}/strategy")
    public PageInfo queryStrategy(UserQueryObject qo) {
        qo.setOrderBy("collectTime desc");
        return strategyService.selectReviewById(qo);
    }

    /**
     * 通过用户的id查询游记
     * @param qo
     * @return
     */
    @GetMapping("{id}/review")
    public PageInfo queryReview(UserQueryObject qo) {
        qo.setOrderBy("collectTime desc");
        return travelService.selectTravelById(qo);
    }







    /**
     * 根据用户id查询对应所有的关注总数
     */
    @GetMapping("/{userId}/attentionsCount")
    public Map getAttentionsById(AttentionQueryObject qo){
        return attentionService.getAttentionsAndFansCountById(qo);
    }

    /**
     * 通过用户的id来查询共有游记的条数
     * @param userId
     * @return
     */
    @GetMapping("travel")
    public int selectCountTravelById(Long userId) {
       int travelCount  =  userService.selectCountTravelById(userId);
        return travelCount;
    }

    /**
     * 通过用户的id来查询共有游记评论的条数
     * @param userId
     * @return
     */
    @GetMapping("/travel/comment")
    public int selectCountTravelCommendById(Long userId) {
       int travelCommendCount  =  userService.selectTravelCommendCountTravelById(userId);
        return travelCommendCount;
    }

    /**
     * 通过用户的id来查询共有游记评论的条数
     * @param userId
     * @return
     */
    @GetMapping("/strategy/comment")
    public int selectCountStrategyCommendById(Long userId) {
       int strategyCommendCount  =  userService.selectStrategyCommendCountStrategyById(userId);
        return strategyCommendCount;
    }
    /**
     * 判断用户是否具有指定的粉丝id
     * @param qo
     * @return 布尔类型
     */
    @GetMapping("/{userId}/attention")
    public boolean isAtention(AttentionQueryObject qo){
        return attentionService.isAttentioin(qo);
    }







    /**********用户订单相关接口 ckw **********/


    /**
     * 当前用户添加商品
     * @param shoppingCar
     * @return
     */
    @PostMapping("shoppingCar")
    public JSONResult addShoppingCar(ShoppingCar shoppingCar){
        shoppingCarService.save(shoppingCar);
        return new JSONResult();
    }


    @GetMapping("getShoppingCar")
    public PageInfo listProductByUserId(ShoppingCarQueryObject qo){
        PageInfo pageInfo = shoppingCarService.query(qo);
        return pageInfo;
    }

    /**
     * 根据id 删除购物车清单
     * @param id
     * @return
     */
    @DeleteMapping("deleteShoppingCar/{id}")
    public JSONResult deleteShoppingCarById(@PathVariable Long id){
        int i = shoppingCarService.deleteByPrimaryKey(id);
        return new JSONResult();
    }

    @PutMapping("changNum")
    public JSONResult changProductNumById(Long id,int number){
        ShoppingCar shoppingCar = new ShoppingCar();
        shoppingCar.setNum(number);
        shoppingCar.setId(id);
        shoppingCarService.updateByPrimaryKey(shoppingCar);
        return new JSONResult();
    }

    @GetMapping("list")
    public List<ShoppingCar> list(Long[] ids){
        List<ShoppingCar> list  = shoppingCarService.getShoppingCarList(ids);
        return list;
    }


}

